home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / UPC12BS1.ZIP / UUCICO / ULIBWIN.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  28KB  |  771 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       u l i b w i n . C                                            */
  3. /*                                                                    */
  4. /*       UUPC/extended serial port communications for Windows 3.1     */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*       Changes copyright (c) 1993, by Robert Denny                  */
  9. /*--------------------------------------------------------------------*/
  10.  
  11. /*--------------------------------------------------------------------*/
  12. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  13. /*       Wonderworks.                                                 */
  14. /*                                                                    */
  15. /*       All rights reserved except those explicitly granted by       */
  16. /*       the UUPC/extended license agreement.                         */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*--------------------------------------------------------------------*/
  20. /*                          RCS Information                           */
  21. /*--------------------------------------------------------------------*/
  22.  
  23. /*
  24.  *    $Id: ulibwin.c 1.7 1993/10/03 22:09:09 ahd Exp $
  25.  *
  26.  *    Revision history:
  27.  *    $Log: ulibwin.c $
  28.  * Revision 1.7  1993/10/03  22:09:09  ahd
  29.  * Use unsigned long to display speed
  30.  *
  31.  * Revision 1.6  1993/10/01  02:36:45  dmwatt
  32.  * Suppress odd debugging messages in normal compile
  33.  * Reformat source
  34.  *
  35.  * Revision 1.5  1993/09/29  13:18:06  ahd
  36.  * Use new dummy setprty function
  37.  *
  38.  * Revision 1.4  1993/09/27  00:48:43  ahd
  39.  * Add dummy set and reset priority functions
  40.  *
  41.  * Revision 1.3  1993/08/02  03:24:59  ahd
  42.  * Further changes in support of Robert Denny's Windows 3.x support
  43.  *
  44.  * Revision 1.2  1993/07/31  16:27:49  ahd
  45.  * Changes in support of Robert Denny's Windows support
  46.  *
  47.  * Revision 1.1  1993/07/22  23:24:23  ahd
  48.  * Initial revision
  49.  *
  50.  */
  51.  
  52. /*--------------------------------------------------------------------*/
  53. /*       Services provided by ulib.c:                                 */
  54. /*                                                                    */
  55. /*          - serial I/O                                              */
  56. /*--------------------------------------------------------------------*/
  57.  
  58. /*--------------------------------------------------------------------*/
  59. /*                        System include files                        */
  60. /*--------------------------------------------------------------------*/
  61.  
  62. #include <stdlib.h>
  63. #include <stdio.h>
  64. #include <string.h>
  65. #include <fcntl.h>
  66. #include <io.h>
  67. #include <time.h>
  68.  
  69. /*--------------------------------------------------------------------*/
  70. /*                    MS Windows 3.1 include files                    */
  71. /*--------------------------------------------------------------------*/
  72.  
  73. #include <windows.h>
  74.  
  75. /*--------------------------------------------------------------------*/
  76. /*                    UUPC/extended include files                     */
  77. /*--------------------------------------------------------------------*/
  78.  
  79. #include "lib.h"
  80. #include "ulib.h"
  81. #include "ssleep.h"
  82. #include "commlib.h"
  83.  
  84. //
  85. // Finally, Microsoft has documented a way to see the Modem Status
  86. // Register bits for modem control lines. This was a real bizarre
  87. // mess with SetCommEventMask() and GetCommEventMask(). The document
  88. // is in the Developer's Knowledge Base:
  89. //
  90. // Title: INF: Accessing the Modem Status Register (MSR) in Windows
  91. // Document Number: Q101417           Publ Date: 15-JUL-1993
  92. // Product Name: Microsoft Windows Software Development Kit
  93. // Product Version:  3.10
  94. // Operating System: WINDOWS
  95. //
  96.  
  97. #define COMM_MSRSHADOW 35          // Offset in DEB of MSR shadow
  98. #define MSR_CTS              0x10  // absolute CTS state in MSR
  99. #define MSR_DSR              0x20  // absolute DSR state in MSR
  100. #define MSR_RI               0x40  // absolute RI state in MSR
  101. #define MSR_RLSD             0x80  // absolute RLSD state in MSR
  102.  
  103. currentfile();
  104.  
  105. static boolean   carrierdetect = FALSE;  /* Modem is not connected     */
  106.  
  107. static boolean hangupNeeded = FALSE;
  108. static UINT currentSpeed = 0;
  109. static LPBYTE lpbModemBits;       // --> Modem Status Register bits
  110.  
  111. #define FAR_NULL ((PVOID) 0L)
  112.  
  113. #define IN_QUEUE_SIZE   2048
  114. #define OUT_QUEUE_SIZE  2048
  115. #define IN_XOFF_LIM     256
  116. #define IN_XON_LIM      256
  117.  
  118. /*--------------------------------------------------------------------*/
  119. /*         Definitions of control structures for Win 3.1 API          */
  120. /*--------------------------------------------------------------------*/
  121.  
  122. static int nCid;
  123. static DCB dcb;
  124.  
  125. /*--------------------------------------------------------------------*/
  126. /*                        Internal prototypes                         */
  127. /*--------------------------------------------------------------------*/
  128.  
  129. static void ShowModem( void );
  130. static void ShowError( int status );
  131.  
  132. /*--------------------------------------------------------------------*/
  133. /*    n o p e n l i n e                                               */
  134. /*                                                                    */
  135. /*    Open the serial port for I/O                                    */
  136. /*--------------------------------------------------------------------*/
  137.  
  138. #ifdef __TURBOC__
  139. #pragma argsused
  140. #endif
  141.  
  142. int nopenline(char *name, BPS baud, const boolean direct )
  143. {
  144.    int rc;
  145.  
  146.    if (portActive)               /* Was the port already active?     ahd   */
  147.       closeline();               /* Yes --> Shutdown it before open  ahd   */
  148.  
  149. #ifdef UDEBUG
  150.    printmsg(15, "openline: %s, %ul", name, (unsigned long) baud);
  151. #endif
  152.  
  153.    if (!equaln(name, "COM", 3 ))
  154.    {
  155.       printmsg(0,
  156.          "openline: Communications port must be format COMx, was %s",
  157.          name);
  158.       return TRUE;
  159.    }
  160.  
  161.    if((nCid = OpenComm(name, IN_QUEUE_SIZE, OUT_QUEUE_SIZE)) < 0)
  162.    {
  163.       printmsg(0, "openline: Failed to open port %s.", name);
  164.       printmsg(0, "nopenline: %s: OpenComm returned %#04X (%d)",
  165.                    name,
  166.                    nCid,
  167.                    nCid);
  168.       return TRUE;
  169.    }
  170.  
  171. /*--------------------------------------------------------------------*/
  172. /*        Get the pointer to the MSR shadow in COMM.DRV's DEB         */
  173. /*--------------------------------------------------------------------*/
  174.  
  175.    lpbModemBits = (LPBYTE)SetCommEventMask(nCid, 0) + COMM_MSRSHADOW;
  176.  
  177. /*--------------------------------------------------------------------*/
  178. /*            Reset any errors on the communications port             */
  179. /*--------------------------------------------------------------------*/
  180.  
  181.    if ((rc = GetCommError (nCid, NULL)) != 0)
  182.    {
  183.       printmsg(0, "openline: Error condition reset on port %s.", name);
  184.       ShowError(rc);
  185.    }
  186.  
  187. /*--------------------------------------------------------------------*/
  188. /*                           Set baud rate                            */
  189. /*--------------------------------------------------------------------*/
  190.  
  191.    nSIOSpeed(baud);
  192.  
  193. /*--------------------------------------------------------------------*/
  194. /*                        Set line attributes                         */
  195. /*--------------------------------------------------------------------*/
  196.  
  197.    printmsg(15,"openline: Getting attributes");
  198.    if ((rc = GetCommState(nCid, &dcb)) != 0)
  199.    {
  200.       printmsg(0,"nopenline: %s: GetCommState was %#04x (%d)",
  201.                   name,
  202.                   rc,
  203.                   rc);
  204.       panic();
  205.    }
  206.  
  207.    dcb.StopBits = ONESTOPBIT;
  208.    dcb.Parity = NOPARITY;
  209.    dcb.ByteSize = 8;
  210.  
  211. /*--------------------------------------------------------------------*/
  212. /*                      Set up for Flow Control                       */
  213. /*--------------------------------------------------------------------*/
  214.  
  215.    printmsg(15,"openline: Disabling XON/XOFF flow